$(function(){
	initNewCoupon();
});

function initNewCoupon() {
    var $document = $(document);
    // nova.ui().render();

    $(".yhfs-add input").on("focus", function(){
        $(this).parents(".yhfs-add").removeClass("yhfs-add-error");
    });

    // 新 添加优惠券或无敌券
    $(".yhfs-add a.btn").on("click", function(){
//        错误提示
//        $this.parents(".yhfs-add").addClass("yhfs-add-error");
        var text = "";
        var objVal = $(this).siblings('input').val();
        if ($.trim(objVal).length == 0) {
            var msg = '请输入优惠券！';
            noticeAlert(msg);
            return false;
        }
        var isload = true;
        //将输入的值和已经查询出来的值想比较，判断是否重复
        var $youhuiquanlist = $('.yhfs-yhq-list-body').find(".yhfs-yhq-list");
        var $youhuiAList = $youhuiquanlist.find('.yhfs-yhq-item');
        $youhuiAList.each(function(i, item){
            var coupCode = $(item).attr('couponcode');
            if($.trim(coupCode) == $.trim(objVal)){
                var msg = '您已经绑定过该券了';
                isload = false;
                noticeAlert(msg);
            }
        });
        var $wudiquanlist = $('.yhfs-wdq-list-body').find(".yhfs-wdq-list");
        var $wudiAList = $wudiquanlist.find('.yhfs-yhq-item');
        $wudiAList.each(function(i, item){
            var coupCode = $(item).attr('couponcode');
            if($.trim(coupCode) == $.trim(objVal)){
                var msg = '您已经绑定过该券了';
                isload = false;
                noticeAlert(msg);
            }
        });
        if (isload) {
            $.ajax({
                type: "post",
                url: "/scenic_front/book/ajax/insertCoupon.do?coupon=" + objVal,
                data: $("form[name='orderForm']").serialize(),
                dataType: "json",
                async: false,
                success: function(data) {
                    if (!data) {
                        // 后台返回空，说明除了异常
                        text = '查询服务异常，请输入其他兑换码';
                        noticeAlert(text);
                        return false;
                    }
                    var myCouponList = data.myCouponList;
                    var errMsg = data.errMessage;
                    if ($.trim(errMsg).length != 0) {
                        if (data.code == "-6") {
                            // 风控领券弹窗
                            $(".risk-management-overlay, .risk-management-dialog").show();
                        } else {
                            noticeAlert(errMsg);
                        }
                        return false;
                    }
                    //循环遍历从后台获取的账户绑定优惠券信息
                    //判断有效
                    if (myCouponList) {
                        // 处理添加成功的券
                        var item = myCouponList[0];
                        if (item.couponCode == objVal && $.trim(errMsg).length > 0) {
                            return false;
                        }
                        if ($.trim(item.validInfo).length ==0) {
                            var $container = $('<a class="yhfs-yhq-item" href="javascript:;">');
                            var $select = $('<span class="yyi-select"></span>');
                            var $price = $('<p class="yyi-price"></p>');
                            var $name = $('<p class="yyi-desc"></p>');
                            var $date = $('<p class="yyi-date"></p>');

                            $container.attr('couponCode', item.couponCode);
                            $container.attr('discountAmount', item.discountAmount);
                            $container.attr('couponNameStr', item.couponNameStr);
                            $price.html('￥' + (item.discountAmount/100).toFixed(2));
                            $name.html(item.couponNameStr);
                            if(!$.isEmptyObject(item.expiredDate) && item.expiredDate != ""){
                                var itemDate = "有效期至 " + item.expiredDate.substr(11,11);
                                $date.html(itemDate);
                                $container.attr('expiredDate', item.expiredDate.substr(11,11));
                            }

                            $container.append($select);
                            $container.append($price);
                            $container.append($name);
                            $container.append($date);

                            var $youhuiquanlist = $('.yhfs-yhq-list-body').find(".yhfs-yhq-list");
                            var $wudiquanlist = $('.yhfs-wdq-list-body').find(".yhfs-wdq-list");
                            var couponCategory = item.couponCategory;
                            if ('ACTIVITY' == couponCategory) {
                                // 数据变化后填充新数据
                                var oldNumYhq;
                                var newNumYhq;
                                var $youhuiBox = $('#youhuiBox');
                                $youhuiquanlist.find('.yhfs-yhq-item-active').removeClass("yhfs-yhq-item-active");
                                // 按顺序放到一个合适的位置
                                $youhuiBox.show();
                                insertEleIntoList($youhuiquanlist, $container);
                                $container.addClass("yhfs-yhq-item-active");
                                //添加成功后，选择新添加券
                                selYouhuiCoupon();
                                //如果是互斥且选择促销，触发选择优惠券，否则触发价格计算
                                var couponExclusionType = $('.couponExclusionType').val();
                                if(couponExclusionType=='1'){
                                    $("#youhuiBox").find(".yhfs-hc-radio").click();//选择优惠券
                                }else {
                                    calcShouldAmt(thisProCategoryId);
                                }

                            } else if ('INVINCIBLE' == couponCategory) {
                                //已经有五张无敌券了，就不再追加
                                var length = $wudiquanlist.find('.yhfs-yhq-item-active').length;
                                text = '添加成功';
                                if (length<5){
                                    var $wudiBox = $('#wudiBox');
                                    $wudiBox.show();
                                    // 按顺序放到一个合适的位置
                                    insertEleIntoList($wudiquanlist, $container);
                                    $container.addClass("yhfs-yhq-item-active");
                                    selWudiCoupon();
                                }else {
                                    insertEleIntoList($wudiquanlist, $container);
                                    text = '添加成功！1个订单最多使用<span class="color-red">5张</span>无敌券，本单使用券数已达上限。';
                                }
                                noticeAlert(text);
                                return;
                            }
                            // 添加成功
                            text = '添加成功';
                            noticeAlert(text);
                        } else {
                            text = item.validInfo;
                            noticeAlert(text);
                        }

                    } else {
                        // 后台返回空，说明除了异常
                        text = '添加失败，请输入其他兑换码';
                        noticeAlert(text);
                    }
                }
            });
        }
    });

    // 优惠券弹窗
    $(".JS-yhq-btn").on("click", function(){
        var singleWidth = 233;
        var dialogWidth = 953;
        // 优惠券数量
        var amount = 5;
        if(amount<4) {
            dialogWidth = 233*amount + 20;
        }
        nova.dialog({
            content: $(".yhfs-yhq-list"),
            title: "优惠券（单选）",
            width: dialogWidth,
            wrapClass: "nova-dialog-yhfs",
            okClassName: "btn-lg btn-orange",
            zIndex: 99,
            okCallback: function(){
                clickYouHuiQuanBtn();
            }
        });
    });

    // 优惠券选择
    $document.on("click", ".yhfs-yhq-list .yhfs-yhq-item", function(){
        var $this = $(this);
        $this.toggleClass("yhfs-yhq-item-active").siblings().removeClass("yhfs-yhq-item-active");
    });

    // 无敌券弹窗
    $(".JS-wdq-btn").on("click", function(){
        var singleWidth = 233;
        var dialogWidth = 953;
        // 优惠券数量
        var amount = 5;
        if(amount<4) {
            dialogWidth = 233*amount + 20;
        }
        nova.dialog({
            content: $(".yhfs-wdq-list"),
            title: "无敌券（多选）",
            width: dialogWidth,
            wrapClass: "nova-dialog-yhfs",
            okClassName: "btn-lg btn-orange",
            zIndex: 99,
            okCallback: function(){
                clickWuDiQuanBtn();
            }
        });
    });

    // 无敌券选择
    $document.on("click", ".yhfs-wdq-list .yhfs-yhq-item", function(){
        var _this =$(this);
        if(_this.hasClass("yhfs-yhq-item-active")){
            $(this).toggleClass("yhfs-yhq-item-active");
            return;
        }
        var $wudiquanlist = _this.parent(".yhfs-wdq-list");
        var $aList = $wudiquanlist.find('.yhfs-yhq-item-active');
        if($aList.length>=5){//选择无敌券不能超过5张
            var text = '1个订单最多使用<span class="color-red">5张</span>无敌券，本单使用券数已达上限。';
            noticeAlert(text);
        }else {
            $(this).toggleClass("yhfs-yhq-item-active");
        }
    });
}
function noticeAlert(text) {
    var icon = "nova-icon-warning";
    if(text&&text.indexOf('成功') > 0){
        icon = "nova-icon-success";
    }
    nova.dialog({
        content: '<div class="wdq-body"><span class="nova-icon-outline-lg '+icon+'"></span>' + text + '</div>',
        title: null,
        width: 470,
        height: 160,
        wrapClass: "nova-dialog-wdqts",
        okText: "知道了",
        okClassName: "btn-lg btn-orange",
        zIndex: 99,
        okCallback: true
    });
}
function setCouponListToPage(myCouponList) {
    //优惠券改版：update by haochi
    var $youhuiBox = $('#youhuiBox');
    var $wudiBox = $('#wudiBox');
    var $jinyongBox = $('#jinyongBox');
    var $youhuiquanlist = $('.yhfs-yhq-list-body').find(".yhfs-yhq-list");
    var $wudiquanlist = $('.yhfs-wdq-list-body').find(".yhfs-wdq-list");
    $youhuiquanlist.html('');
    $wudiquanlist.html('');
    if(myCouponList != null && myCouponList.length > 0){
        var youhuiquanArray = [];
        var wudiquanArray = [];
        $.each(myCouponList, function(k, item){
            var couponCategory = item.couponCategory;
            if ('ACTIVITY' == couponCategory) {
                youhuiquanArray.push(item);
            } else if ('INVINCIBLE' == couponCategory) {
                wudiquanArray.push(item);
            }
            // todo: 自测用的，测完删掉
            // youhuiquanArray.push(item);
            // wudiquanArray.push(item);
        });
        //清空列表中不存在的优惠券
        clearYouhuiQuan(youhuiquanArray);
        if (youhuiquanArray.length > 0) {
            // $youhuiBox.find('.yhl-price').html(myCouponList.length + '张可用');
            $youhuiBox.show();
            // 对优惠券进行排序
            sortCouponList(youhuiquanArray);
        }
        //清空列表中不存在的无敌券
        clearWudiQuan(wudiquanArray);
        if (wudiquanArray.length > 0) {
            $wudiBox.find('.yhl-price').html(wudiquanArray.length + '张可用');
            $wudiBox.show();
            // 对无敌券进行排序
            sortCouponList(wudiquanArray);
        }

        $.each(youhuiquanArray, function(k, item){
            // 遍历优惠券
            var $container;
            if (k === 0) {
                $container = $('<a class="yhfs-yhq-item yhfs-yhq-item-active" href="javascript:;"></a>');
            } else {
                $container = $('<a class="yhfs-yhq-item" href="javascript:;"></a>');
            }
            var $select = $('<span class="yyi-select"></span>');
            var $price = $('<p class="yyi-price"></p>');
            var $name = $('<p class="yyi-desc"></p>');
            var $date = $('<p class="yyi-date"></p>');

            $container.attr('couponCode', item.couponCode);
            $container.attr('discountAmount', item.discountAmount);
            $container.attr('couponNameStr', item.couponNameStr);
            $price.html('￥' + (item.discountAmount/100).toFixed(2));
            $name.html(item.couponNameStr);
            if(!$.isEmptyObject(item.expiredDate) && item.expiredDate != ""){
                var itemDate = "有效期至 " + item.expiredDate.substr(11,11);
                $date.html(itemDate);
                $container.attr('expiredDate', item.expiredDate.substr(11,11));
            }

            $container.append($select);
            $container.append($price);
            $container.append($name);
            $container.append($date);
            $youhuiquanlist.append($container);
        });

        $.each(wudiquanArray, function(k, item){
            // 遍历无敌券
            var $container = $('<a class="yhfs-yhq-item" href="javascript:;">');
            var $select = $('<span class="yyi-select"></span>');
            var $price = $('<p class="yyi-price"></p>');
            var $name = $('<p class="yyi-desc"></p>');
            var $date = $('<p class="yyi-date"></p>');

            $container.attr('couponCode', item.couponCode);
            $container.attr('discountAmount', item.discountAmount);
            $container.attr('couponNameStr', item.couponNameStr);
            $price.html('￥' + (item.discountAmount/100).toFixed(2));
            $name.html(item.couponNameStr);
            if(!$.isEmptyObject(item.expiredDate) && item.expiredDate != ""){
                var itemDate = "有效期至 " + item.expiredDate.substr(11,11);
                $date.html(itemDate);
                $container.attr('expiredDate', item.expiredDate.substr(11,11));
            }

            $container.append($select);
            $container.append($price);
            $container.append($name);
            $container.append($date);
            $wudiquanlist.append($container);
        });

        if (youhuiquanArray.length > 0) {
            // 有优惠券的话，默认选中第一个后，展示到列表中
            //clickYouHuiQuanBtn();
            //同步弹框的选中状态
            var $dialogyouhuiquanlist = $('.nova-dialog-body').find(".yhfs-yhq-list");
            var $youhuiquanlist = $('.yhfs-yhq-list-body').find(".yhfs-yhq-list");
            var $dialogAList = $dialogyouhuiquanlist.find('.yhfs-yhq-item');
            var $aList = $youhuiquanlist.find('.yhfs-yhq-item');
            var quanNum = $aList.length;
            $dialogAList.each(function(i, item){
                var $ele = $($aList[i]);
                if ($(item).hasClass('yhfs-yhq-item-active')) {
                    if (!$ele.hasClass('yhfs-yhq-item-active')) {
                        $ele.addClass('yhfs-yhq-item-active');
                    }
                } else {
                    if ($ele.hasClass('yhfs-yhq-item-active')) {
                        $ele.removeClass('yhfs-yhq-item-active');
                    }
                }
            });

            var $youhuiBox = $('#youhuiBox');
            var $selectedQuans = $youhuiquanlist.find('.yhfs-yhq-item-active');
            var $priceNumContainer = $youhuiBox.find('.yhl-price');
            $priceNumContainer.empty();
            var $selYhqContainer = $youhuiBox.find('.yhfs-center');
            $selYhqContainer.empty();
            if ($selectedQuans != null && $selectedQuans.length > 0) {
                var $selQuan = $($selectedQuans[0]);
                var couponCode = $selQuan.attr('couponCode');
                var discountAmount = $selQuan.attr('discountAmount');
                var couponNameStr = $selQuan.attr('couponNameStr');
                var $p = $('<p></p>');
                var $em = $('<em></em>');
                $em.append('-￥' + (discountAmount/100).toFixed(2));
                $p.html(couponNameStr);
                $p.append($em);
                $p.attr('couponCode', couponCode);
                $p.attr('discountAmount', discountAmount);
                $selYhqContainer.append($p);
                var $em2 = $('<em></em>');
                $em2.append((discountAmount/100).toFixed(2));
                $priceNumContainer.html('-￥');
                $priceNumContainer.append($em2);
            } else {
                $priceNumContainer.html(quanNum + '张可用');
            }
        }
    }
}

function clickYouHuiQuanBtn() {
    //同步弹框的选中状态
    var $dialogyouhuiquanlist = $('.nova-dialog-body').find(".yhfs-yhq-list");
    var $youhuiquanlist = $('.yhfs-yhq-list-body').find(".yhfs-yhq-list");
    var $dialogAList = $dialogyouhuiquanlist.find('.yhfs-yhq-item');
    var $aList = $youhuiquanlist.find('.yhfs-yhq-item');
    var quanNum = $aList.length;
    $dialogAList.each(function(i, item){
        var $ele = $($aList[i]);
        if ($(item).hasClass('yhfs-yhq-item-active')) {
            if (!$ele.hasClass('yhfs-yhq-item-active')) {
                $ele.addClass('yhfs-yhq-item-active');
            }
        } else {
            if ($ele.hasClass('yhfs-yhq-item-active')) {
                $ele.removeClass('yhfs-yhq-item-active');
            }
        }
    });

    var $youhuiBox = $('#youhuiBox');
    var $selectedQuans = $youhuiquanlist.find('.yhfs-yhq-item-active');
    var $priceNumContainer = $youhuiBox.find('.yhl-price');
    $priceNumContainer.empty();
    var $selYhqContainer = $youhuiBox.find('.yhfs-center');
    $selYhqContainer.empty();
    if ($selectedQuans != null && $selectedQuans.length > 0) {
        var $selQuan = $($selectedQuans[0]);
        var couponCode = $selQuan.attr('couponCode');
        var discountAmount = $selQuan.attr('discountAmount');
        var couponNameStr = $selQuan.attr('couponNameStr');
        var $p = $('<p></p>');
        var $em = $('<em></em>');
        $em.append('-￥' + (discountAmount/100).toFixed(2));
        $p.html(couponNameStr);
        $p.append($em);
        $p.attr('couponCode', couponCode);
        $p.attr('discountAmount', discountAmount);
        $selYhqContainer.append($p);
        var $em2 = $('<em></em>');
        $em2.append((discountAmount/100).toFixed(2));
        $priceNumContainer.html('-￥');
        $priceNumContainer.append($em2);
    } else {
        $priceNumContainer.html(quanNum + '张可用');
    }
    calcShouldAmt(thisProCategoryId);
}

function clickWuDiQuanBtn() {
    //同步弹框的选中状态
    var $dialogyouhuiquanlist = $('.nova-dialog-body').find(".yhfs-wdq-list");
    var $wudiquanlist = $('.yhfs-wdq-list-body').find(".yhfs-wdq-list");
    var $dialogAList = $dialogyouhuiquanlist.find('.yhfs-yhq-item');
    var $aList = $wudiquanlist.find('.yhfs-yhq-item');
    var quanNum = $aList.length;
    $dialogAList.each(function(i, item){
        var $ele = $($aList[i]);
        if ($(item).hasClass('yhfs-yhq-item-active')) {
            if (!$ele.hasClass('yhfs-yhq-item-active')) {
                $ele.addClass('yhfs-yhq-item-active');
            }
        } else {
            if ($ele.hasClass('yhfs-yhq-item-active')) {
                $ele.removeClass('yhfs-yhq-item-active');
            }
        }
    });

    var $wudiBox = $('#wudiBox');
    var $selectedQuans = $wudiquanlist.find('.yhfs-yhq-item-active');
    var $priceNumContainer = $wudiBox.find('.yhl-price');
    $priceNumContainer.empty();
    var $selYhqContainer = $wudiBox.find('.yhfs-center');
    $selYhqContainer.empty();
    if ($selectedQuans != null && $selectedQuans.length > 0) {
        var totalDiscount = 0;
        $selectedQuans.each(function(i, item){
            var $selQuan = $(item);
            var couponCode = $selQuan.attr('couponCode');
            var discountAmount = $selQuan.attr('discountAmount');
            var couponNameStr = $selQuan.attr('couponNameStr');
            var $p = $('<p></p>');
            var $em = $('<em></em>');
            $em.append('-￥' + (discountAmount/100).toFixed(2));
            $p.html(couponNameStr);
            $p.append($em);
            $p.attr('couponCode', couponCode);
            $p.attr('discountAmount', discountAmount);
            $selYhqContainer.append($p);
            totalDiscount += Number(discountAmount);
        });
        var $em2 = $('<em></em>');
        $em2.append((totalDiscount/100).toFixed(2));
        $priceNumContainer.html('-￥');
        $priceNumContainer.append($em2);
    } else {
        $wudiBox.find('.yhl-price').html(quanNum + '张可用');
    }
    calcShouldAmt(thisProCategoryId);
}
//优惠券选择
function selYouhuiCoupon() {
    //同步弹框的选中状态
    var $youhuiquanlist = $('.yhfs-yhq-list-body').find(".yhfs-yhq-list");
    var $aList = $youhuiquanlist.find('.yhfs-yhq-item');
    var quanNum = $aList.length;
    var $youhuiBox = $('#youhuiBox');
    var $selectedQuans = $youhuiquanlist.find('.yhfs-yhq-item-active');
    var $priceNumContainer = $youhuiBox.find('.yhl-price');
    $priceNumContainer.empty();
    var $selYhqContainer = $youhuiBox.find('.yhfs-center');
    $selYhqContainer.empty();
    if ($selectedQuans != null && $selectedQuans.length > 0) {
        var $selQuan = $($selectedQuans[0]);
        var couponCode = $selQuan.attr('couponCode');
        var discountAmount = $selQuan.attr('discountAmount');
        var couponNameStr = $selQuan.attr('couponNameStr');
        var $p = $('<p></p>');
        var $em = $('<em></em>');
        $em.append('-￥' + (discountAmount/100).toFixed(2));
        $p.html(couponNameStr);
        $p.append($em);
        $p.attr('couponCode', couponCode);
        $p.attr('discountAmount', discountAmount);
        $selYhqContainer.append($p);
        var $em2 = $('<em></em>');
        $em2.append((discountAmount/100).toFixed(2));
        $priceNumContainer.html('-￥');
        $priceNumContainer.append($em2);
    } else {
        $priceNumContainer.html(quanNum + '张可用');
    }
}
//无敌券选择--
function selWudiCoupon() {
        var $wudiquanlist = $('.yhfs-wdq-list-body').find(".yhfs-wdq-list");
        var $aList = $wudiquanlist.find('.yhfs-yhq-item');
        var quanNum = $aList.length;
        var $wudiBox = $('#wudiBox');
        var $selectedQuans = $wudiquanlist.find('.yhfs-yhq-item-active');
        var $priceNumContainer = $wudiBox.find('.yhl-price');
        $priceNumContainer.empty();
        var $selYhqContainer = $wudiBox.find('.yhfs-center');
        $selYhqContainer.empty();
        if ($selectedQuans != null && $selectedQuans.length > 0) {
            var totalDiscount = 0;
            $selectedQuans.each(function(i, item){
                var $selQuan = $(item);
                var couponCode = $selQuan.attr('couponCode');
                var discountAmount = $selQuan.attr('discountAmount');
                var couponNameStr = $selQuan.attr('couponNameStr');
                var $p = $('<p></p>');
                var $em = $('<em></em>');
                $em.append('-￥' + (discountAmount/100).toFixed(2));
                $p.html(couponNameStr);
                $p.append($em);
                $p.attr('couponCode', couponCode);
                $p.attr('discountAmount', discountAmount);
                $selYhqContainer.append($p);
                totalDiscount += Number(discountAmount);
            });
            var $em2 = $('<em></em>');
            $em2.append((totalDiscount/100).toFixed(2));
            $priceNumContainer.html('-￥');
            $priceNumContainer.append($em2);
        } else {
            $wudiBox.find('.yhl-price').html(quanNum + '张可用');
        }
        calcShouldAmt(thisProCategoryId);
}

//优惠券初始化，当订单变化时，优惠券不存在的，从列表中清除
function clearYouhuiQuan(array) {
    var $youhuiBox = $('#youhuiBox');
    if(array&&array.length>0){
        $youhuiBox.find('.yhfs-center').empty();
    }else {
        $youhuiBox.find('.yhfs-center').empty();
        $youhuiBox.hide();
    }

}
//无敌券初始化，当订单变化时，无敌券不存在的，从列表中清除
function clearWudiQuan(array) {
    var $wudiBox = $('#wudiBox');
    if(array&&array.length>0){
        $wudiBox.find('.yhfs-center').empty();
        $wudiBox.find('.yhl-price').html(array.length + '张可用');
    }else {
        $wudiBox.find('.yhfs-center').empty();
        $wudiBox.hide();
    }

}
function sortCouponList(coupList) {
    for (var i = 0; i < coupList.length; i++) {
        for (var j = 0; j < coupList.length - i - 1; j++) {
            if(coupList[j].discountAmount == coupList[j + 1].discountAmount){
                var date1 = new Date(coupList[j].expiredDate.substr(11,10));
                var date2 = new Date(coupList[j + 1].expiredDate.substr(11,10));
                if(Date.parse(date1) - Date.parse(date2) > 0){
                    var temp= coupList[j];
                    coupList[j] = coupList[j + 1];
                    coupList[j + 1] = temp;
                }

            }
            if (coupList[j].discountAmount < coupList[j + 1].discountAmount) {
                var temp= coupList[j];
                coupList[j] = coupList[j + 1];
                coupList[j + 1] = temp;
            }
        }
    }
}

function insertEleIntoList($quanlist, $container) {
    var curDiscountAmount = Number($container.attr('discountAmount'));
    var curExpiredDate = Date.parse(new Date($container.attr('expiredDate')));
    var index = -1;
    $quanlist.find('.yhfs-yhq-item').each(function(i, item){
        var $item = $(item);
        var discountAmount = Number($item.attr('discountAmount'));
        var expiredDate = Date.parse(new Date($item.attr('expiredDate')));
        if (curDiscountAmount == discountAmount) {
            if (curExpiredDate <= expiredDate) {
                index = i;
                $item.before($container);
                return false;
            }
        } else if (curDiscountAmount > discountAmount) {
            index = i;
            $item.before($container);
            return false;
        }
    });
    if (index < 0) {
        $quanlist.append($container);
    }
}

function setPromtionsToPage(promList, couponExclusion, totalPromDiscount) {
    var $cuxiaoBox = $('#cuxiaoBox');
    var $youhuiBox = $('#youhuiBox');

    if (promList != null && promList.length > 0) {
        $cuxiaoBox.show();
        $cuxiaoBox.find('.yhfs-center').html('');
        $cuxiaoBox.find('.hidden-promotion-list').html('');
        var item;
        for (var i = 0; i < promList.length; i++) {
            item = promList[i];
            var $p = $('<p class="yhl-cx-selected"></p>');
            var $em = $('<em></em>');
            $em.append('-￥' + (item.discountAmount ? (item.discountAmount/100).toFixed(2) : 0.00));
            $p.append(item.title);
            $p.append($em);
            $p.attr('key', item.key);
            $p.attr('promPromotionId', item.promPromotionId);
            $cuxiaoBox.find('.yhfs-center').append($p);
            // 设置隐藏域
            // var $hidden = $("<input type='hidden' class='promotionMap' name='promotionMap["+item.key+"]' value='"+item.promPromotionId+"'>");
        }
        // var totalPromDiscount = 501;
        $cuxiaoBox.find('.yhfs-left').find('em').html((totalPromDiscount/100).toFixed(2));
        $cuxiaoBox.find('.yhfs-left').find('.yhl-price').attr('totalPromDiscount', totalPromDiscount);
        // 有促销才会去判断到底互斥不互斥
        if (couponExclusion) {
            $cuxiaoBox.addClass('yhfs-item-hc');
            $youhuiBox.addClass('yhfs-item-hc');
            var couponExclusionType = $('.couponExclusionType').val();
            if (couponExclusionType == '-1' || couponExclusionType == '0') {
                // 促销与优惠券互斥
                var totalYouhuiquanAmount = getYouhuiquanAmount();
                if (totalPromDiscount >= totalYouhuiquanAmount) {
                    $('.couponExclusionType').val('1');
                    $cuxiaoBox.addClass('yhfs-item-active');
                    $youhuiBox.removeClass('yhfs-item-active');
                } else {
                    $('.couponExclusionType').val('2');
                    $cuxiaoBox.removeClass('yhfs-item-active');
                    $youhuiBox.addClass('yhfs-item-active');
                }
                if($('.yhfs-hc-radio').length==0) {//当存在时，就不重复添加了
                    $cuxiaoBox.prepend($('<span class="yhfs-hc-radio"></span>'));
                    $youhuiBox.prepend($('<span class="yhfs-hc-radio"></span>'));
                    // 优惠，促销互斥选择
                    $(".yhfs-hc-radio").on("click", function () {
                        var $this = $(this);
                        var $thisItem = $this.parents(".yhfs-item-hc");
                        if (!$thisItem.hasClass('yhfs-item-active')) {
                            $this.parents(".yhfs-content").find(".yhfs-item-hc").removeClass("yhfs-item-active");
                            $thisItem.addClass("yhfs-item-active");
                            var name = $thisItem.attr('name');
                            var couponExclusionType = '1';
                            if (name == 'cuxiao') {
                                couponExclusionType = '1';
                                usePromotion = true;
                            } else if (name = 'youhui') {
                                couponExclusionType = '2';
                                useCoupon = true;
                            }
                            $('.couponExclusionType').val(couponExclusionType);
                            calcShouldAmt(thisProCategoryId);
                        }

                    });
                }
            }

        } else {
            $cuxiaoBox.removeClass('yhfs-item-hc');
            $youhuiBox.removeClass('yhfs-item-hc');
            // 促销与优惠券不互斥
            $('.couponExclusionType').val('0');
            $cuxiaoBox.find('.yhfs-hc-radio').remove();
            $youhuiBox.find('.yhfs-hc-radio').remove();
            $cuxiaoBox.removeClass('yhfs-item-active');
            $youhuiBox.removeClass('yhfs-item-active');
        }
    } else {
        // 没有促销，是否互斥已经没有意义了
        $('.couponExclusionType').val('0');
        $cuxiaoBox.find('.yhfs-hc-radio').remove();
        $youhuiBox.find('.yhfs-hc-radio').remove();
        $cuxiaoBox.removeClass('yhfs-item-active');
        $youhuiBox.addClass('yhfs-item-active');

        $cuxiaoBox.hide();
        $cuxiaoBox.find('.yhfs-center').html('');
    }

}

//设置促销隐藏域
function setPromitionHidden() {
    var $cuxiaoBox = $('#cuxiaoBox');
    var promsSelected = $cuxiaoBox.find('.yhfs-center').find('.yhl-cx-selected');
    promsSelected.each(function(i, item){
        var $this = $(item);
        var key = $this.attr('key');
        var promPromotionId = $this.attr('promPromotionId');
        if(promPromotionId != "" && promPromotionId != null) {
            var hiddenInput = $("<input type='hidden' class='promotionMap' name='promotionMap[" + key + "]'>");
            hiddenInput.val(promPromotionId);
            $cuxiaoBox.find('.hidden-promotion-list').append(hiddenInput);
        }
    });
}

function getYouhuiquanAmount() {
    var $youhuiBox = $('#youhuiBox');
    var totalYouhuiquanAmount = 0;
    $youhuiBox.find('.yhfs-center').find('p').each(function(i, item){
        var $this = $(item);
        var discountAmount = $this.attr('discountAmount');
        if(discountAmount != "" && discountAmount != null) {
            totalYouhuiquanAmount += parseFloat(discountAmount);
        }
    });
    return totalYouhuiquanAmount;
}
